package com.example.demo.service;

import com.example.demo.dao.ColumnArticleDao;
import com.example.demo.pojo.ColumnArticle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by hasee
 * on 2019/10/29 10:50
 */
@Service("columnArticleService")
@Transactional
public class ColumnArticleServiceImpl implements ColumnArticleService {
    @Autowired
    private ColumnArticleDao columnArticleDao;

    /**
     * 查询专栏列表
     * **/
    public List<ColumnArticle> selectColumnList(int start){

        List<ColumnArticle> msgFromDao=new ArrayList<>();

        System.out.println("得到前端传递的数字"+start+",查询中...");
        msgFromDao = columnArticleDao.selectColumnList(start);
        System.out.println("得到结果："+msgFromDao);
        try {
            if(msgFromDao != null){
                System.out.println("查询列表成功");
            }else{
                msgFromDao = null;
                System.out.println("查询列表失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("selectColumnList方法中出现空指针异常");
        }
        return msgFromDao;
    };

    /**
     * 查询专栏文章相信信息
     * **/
    @Override
    public ColumnArticle selectColumnInfo(ColumnArticle columnArticle){
        ColumnArticle resultFromDao=new ColumnArticle();
        int result = 0;
        String a;
        System.out.println("得到前端传递的文章ID"+columnArticle+",查询中...");
        System.out.println(columnArticle);
        try {
            resultFromDao = columnArticleDao.selectColumnInfo(columnArticle.getArticleId());
            System.out.println(columnArticle.getArticleId());
            System.out.println(columnArticleDao.selectColumnInfo(columnArticle.getArticleId()));
            System.out.println("得到结果："+resultFromDao);

            if(resultFromDao != null){
                result = 1;
                System.out.println("查询内容成功");
            }else{
                result = 0;
                System.out.println("查询内容失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("selectColumnInfo方法中出现空指针异常");
            resultFromDao = null;
        }
        return resultFromDao;
    }

    @Override
    public int saveUploadColumnInfo(ColumnArticle columnArticle) {
        int row=0;
        row=columnArticleDao.saveUploadColumnInfo(columnArticle);
        if(row>0){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 查询所有专栏，无select条件/get请求
     * **/
    @Override
    public List<ColumnArticle> allArticleList() {
        List<ColumnArticle> articleList = null;
        try {
            articleList = columnArticleDao.allArticleList();
            if(articleList == null){
                articleList = null;
                return articleList;
            }else{
                return articleList;
            }
        }catch (Exception e){
            articleList = null;
        }
        return articleList;
    }
}
